import { OnInit, Component, Input, ViewChild } from '@angular/core';
import { NzModalRef, NzMessageService, NzNotificationService } from 'ng-zorro-antd';
import { SFSchema, SFUISchema, SFComponent } from '@delon/form';
import { _HttpClient } from '@delon/theme';

/**
 * 添加/编辑权限按钮对话框
 */
@Component({
  selector: 'permission-edit-modal',
  templateUrl: './permission-modal.component.html',
})
export class PermissionModalComponent implements OnInit {
  @Input() record: any = {};
  i: any;
  @Input()
  parent;
  schema: SFSchema = { properties: {} };
  @ViewChild('sf', { static: false }) sf: SFComponent;

  constructor(
    private modal: NzModalRef,
    private msgSrv: NzMessageService,
    public http: _HttpClient,
    private notice: NzNotificationService,
  ) {}

  ngOnInit(): void {
    if (!this.record.id) {
      this.initSchema(this.parent.key);
    } else {
      this.http.get(`/cf/admin/menu/${this.record.id}`).subscribe(res => {
        if (res.status === 200) {
          this.initSchema2(res.data);
        }
      });
    }
  }

  save(value: any) {
    if (!this.record.id) {
      this.http.post(`/cf/admin/menu`, value).subscribe(res => {
        if (res.status === 200) {
          this.notice.success('成功', '保存成功');
          this.modal.close(true);
        }
      });
    } else {
      this.http.put(`/cf/admin/menu`, value).subscribe(res => {
        if (res.status === 200) {
          this.notice.success('成功', '保存成功');
          this.modal.close(true);
        }
      });
    }
  }

  close() {
    this.modal.destroy();
  }

  private initSchema2(data) {
    this.schema = {
      properties: {
        menuId: {
          type: 'string',
          title: '菜单id',
          default: data.menuId,
          ui: {
            hidden: true,
          },
        },
        delFlag: {
          type: 'string',
          title: '状态',
          default: data.delFlag,
          ui: {
            hidden: true,
          },
        },
        type: {
          type: 'string',
          title: '类型',
          default: data.type,
          ui: {
            hidden: true,
          },
        },
        parentId: {
          type: 'string',
          title: '父级节点',
          readOnly: true,
          default: data.parentId,
          ui: {
            hidden: true,
          },
        },
        permission: { type: 'string', title: '按钮编码', default: data.permission },
        name: { type: 'string', title: '按钮名称', default: data.name },
        description: { type: 'string', title: '描述', maxLength: 50, default: data.description },
      },
      required: ['permission', 'name'],
    };
    this.sf.refreshSchema();
  }

  private initSchema(parentId) {
    this.schema = {
      properties: {
        menuId: {
          type: 'string',
          title: '菜单id',
          ui: {
            hidden: true,
          },
        },
        delFlag: {
          type: 'string',
          title: '状态',
          default: '0',
          ui: {
            hidden: true,
          },
        },
        type: {
          type: 'string',
          title: '类型',
          default: '1',
          ui: {
            hidden: true,
          },
        },
        parentId: {
          type: 'string',
          title: '父级节点',
          readOnly: true,
          default: parentId,
          ui: {
            hidden: true,
          },
        },
        permission: { type: 'string', title: '按钮编码' },
        name: { type: 'string', title: '按钮名称' },
        description: { type: 'string', title: '描述', maxLength: 50 },
      },
      required: ['permission', 'name'],
    };
  }
}
